home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / LIB / UNIXLIB37B / !UnixLib37 / src / time / c / gettod < prev    next >
Text File  |  1996-11-09  |  2KB  |  79 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source: /unixb/home/unixlib/source/unixlib37/src/time/c/RCS/gettod,v $
  4.  * $Date: 1996/05/06 09:01:35 $
  5.  * $Revision: 1.2 $
  6.  * $State: Rel $
  7.  * $Author: unixlib $
  8.  *
  9.  * $Log: gettod,v $
  10.  * Revision 1.2  1996/05/06 09:01:35  unixlib
  11.  * Updates to sources made by Nick Burrett, Peter Burwood and Simon Callan.
  12.  * Saved for 3.7a release.
  13.  *
  14.  * Revision 1.1  1996/04/19 21:35:00  simon
  15.  * Initial revision
  16.  *
  17.  ***************************************************************************/
  18.  
  19. static const char rcs_id[] = "$Id: gettod,v 1.2 1996/05/06 09:01:35 unixlib Rel $";
  20.  
  21. #include <errno.h>
  22. #include <time.h>
  23. #include <sys/time.h>
  24.  
  25.  
  26. /* The `gettimeofday' function returns the current date and time in
  27.    the `struct timeval' structure indicated by TP.  Information about
  28.    the time zone is returned in the structure pointed at TZP.  If the
  29.    TZP argument is a null pointer, time zone information is ignored.
  30.  
  31.    The return value is `0' on success and `-1' on failure.  The
  32.    following `errno' error condition is defined for this function:
  33.  
  34.    `ENOSYS'
  35.    The operating system does not support getting time zone
  36.    information, and TZP is not a null pointer.  The GNU
  37.    operating system does not support using `struct timezone' to
  38.    represent time zone information; that is an obsolete feature
  39.    of 4.3 BSD.  */
  40.  
  41.  
  42. /* Get the current time of day and timezone information,
  43.    putting it into *TV and *TZ.  If TZ is NULL, *TZ is not filled.
  44.    Returns 0 on success, -1 on errors.  */
  45. int
  46. gettimeofday (struct timeval *tv, struct timezone *tz)
  47. {
  48.   if (tv == NULL)
  49.     {
  50.       errno = EINVAL;
  51.       return -1;
  52.     }
  53.  
  54.   tv->tv_sec = time ((time_t *) NULL);
  55.   tv->tv_usec = 0;
  56.  
  57.   if (tz != NULL)
  58.     {
  59.       const time_t timer = tv->tv_sec;
  60.       const struct tm *tm;
  61.  
  62.       const int save_timezone = timezone;
  63.       const struct tm saved_tz = __tz[0];
  64.  
  65.       tm = localtime (&timer);
  66.  
  67.       tz->tz_minuteswest = timezone / 60;
  68.       tz->tz_dsttime = 0;
  69.  
  70.       timezone = save_timezone;
  71.       __tz[0] = saved_tz;
  72.  
  73.       if (tm == NULL)
  74.     return -1;
  75.     }
  76.  
  77.   return 0;
  78. }
  79.